import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from './router'
import axios from "axios";
import Vuex from 'vuex';
import store from './store/main'
import BaiduMap from 'vue-baidu-map' //引入百度地图插件

Vue.prototype.$axios = axios    //全局注册，使用方法为:this.$axios
//引入 echarts
import * as echarts from 'echarts'
//注册组件
Vue.prototype.$echarts = echarts

Vue.config.productionTip = false
Vue.use(ElementUI)

Vue.use(Vuex);

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')


Vue.use(BaiduMap, {
    ak: 'lDxLtnCywzSSgZGVIV7Wi0xwfDNGsq6y'
})

// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
    config => {
      if (localStorage.getItem('token')) {
        config.headers.token = localStorage.getItem('token');
      }

      return config;
    },
    error => {
      return Promise.reject(error);
    });



//respone拦截器
axios.interceptors.response.use(
    response => {
      return response;
    },

    error => {  //默认除了2XX之  外都为错误
      if(error.response){
        switch(error.response.data.status){
          case 401:
            store.commit('delToken');
            router.replace({ //跳转到登录页面
              path: '/',
              query: { redirect: router.currentRoute.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
            });
        }
      }
      return Promise.reject(error.response);
    }
);



